<?php

App::uses('AppModel', 'Model');

/**
 * Organisation Model
 *
 * @property OrganisationType $OrganisationType
 * @property Member $Member
 * @property OrganisationAccount $OrganisationAccount
 * @property OrganisationContributionType $OrganisationContributionType
 * @property OrganisationAnniversary $OrganisationAnniversary
 * @property OrganisationGroupType $OrganisationGroupType
 * @property OrganisationSubscription $OrganisationSubscription
 */
class Organisation extends AppModel {
    //The Associations below have been created with all possible keys, those that are not needed can be removed

    public $useDbConfig = "organisations";
    
    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'OrganisationType' => array(
            'className' => 'OrganisationType',
            'foreignKey' => 'organisation_type_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    /**
     * hasMany associations
     *
     * @var array
     */
    public $hasMany = array(
        'OrganisationAccount' => array(
            'className' => 'OrganisationAccount',
            'foreignKey' => 'organisation_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'OrganisationContributionType' => array(
            'className' => 'OrganisationContributionType',
            'foreignKey' => 'organisation_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'OrganisationAnniversary' => array(
            'className' => 'OrganisationAnniversary',
            'foreignKey' => 'organisation_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'OrganisationGroupType' => array(
            'className' => 'OrganisationGroupType',
            'foreignKey' => 'organisation_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'OrganisationSubscription' => array(
            'className' => 'OrganisationSubscription',
            'foreignKey' => 'organisation_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'OrganisationSetting' => array(
            'className' => 'OrganisationSetting',
            'foreignKey' => 'organisation_id',
            'dependent' => false
        ),
        'OrganisationMember' => array(
            'className' => 'OrganisationMember',
            'foreignKey' => 'organisation_id',
            'dependent' => false
        ),
        'OrganisationMemberImage' => array(
            'className' => 'OrganisationMemberImage',
            'foreignKey' => 'organisation_id',
            'dependent' => false
        ),
        'OrganisationRole' => array(
            'className' => 'OrganisationRole',
            'foreignKey' => 'organisation_id',
            'dependent' => false
        )

    );
    
    public function getOrganisationInfo($organisation_id, $contain = array()) {
        
        if( empty($contain) ) {
            $contain = array(
                'OrganisationAnniversary' => array(
                    'conditions' => array('active' => 1)
                ), 
                'OrganisationGroupType' => array(
                    'conditions' => array('active' => 1),
                    'OrganisationGroup' => array(
                        'conditions' => array('active' => 1)
                    )
                )
            );
        }
        
        return $this->find('first', array(
            'contain' => $contain,
            'fields' => array('id', 'name', 'address', 'email', 'phone', 'website'),
            'conditions' => array(
                'Organisation.id' => $organisation_id,
                'Organisation.active' => 1
            ),
            'recursive' => $contain ? 2 : -1
        ));
    }

}
